# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgname=pacman
pkgver=4.2.1.6258.f5bbd79
pkgrel=1
pkgdesc="A library-based package manager with dependency support (MSYS2 port)"
arch=('i686' 'x86_64')
url="http://www.archlinux.org/pacman/"
license=('GPL')
groups=('base' 'base-devel')
depends=('bash>=4.2.045'
         #'dash'
         #'busybox'
         'gettext'
         'gnupg'
         'msys2-runtime'
         #'libarchive'
         'curl'
         #'libgpgme'
         'pacman-mirrors'
         'msys2-keyring'
         'which'
         'bzip2'
         'xz')
optdepends=('diffutils' 'vim')
checkdepends=('python2')
makedepends=('asciidoc'
             'autoconf'
             'automake-wrapper'
             'doxygen'
             'libtool'
             'git'
             'gettext-devel'
             'libarchive-devel'
             'libcurl-devel'
             'libgpgme-devel')
provides=('pacman-contrib')
conflicts=('pacman-contrib')
replaces=('pacman-contrib')
# options=('!strip' 'debug')
backup=("etc/pacman.conf"
        "etc/makepkg.conf"
        "etc/makepkg_mingw32.conf"
        "etc/makepkg_mingw64.conf")
source=("$pkgname"::'git+https://github.com/Alexpux/MSYS2-pacman.git'
        "pacman.conf"
        "makepkg.conf"
        "makepkg_mingw32.conf"
        "makepkg_mingw64.conf"
        "makepkg-mingw"
        "0001-more-debugging-info.patch"
        "0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch"
        "0003-use-busybox-for-msys2-post-installs.patch"
        "0004-Link-pacman-with-static-libraries.patch")
md5sums=('SKIP'
         'a74be5548ab4743ef05712ebff0b8472'
         'fc9e737a24f08f842f1f4c54bdcaa653'
         '71a1ed1ce89fe81e1ca88dfc959bab49'
         'ea9c9637a8a6d5b706579d237ed56d7a'
         'f284fbfe409cac2cb56972aa6956cacc'
         '3478ac5ab27b8cdb289d1ac3792b16ec'
         'feb2bcc62db05f7bc6ac97d43dd0643a'
         '9ee3ca289031ca9c92db2805b75c1944'
         '0f67ea93a9b0f1a39b609f3c3a506bfb')

pkgver() {
  cd "${srcdir}/${pkgname}"
  #printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  printf "%s.%s.%s" "$(git describe --tags --abbrev=0 | sed 's/^v//')" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/$pkgname
  git am "${srcdir}"/0001-more-debugging-info.patch
  #git am "${srcdir}"/0002-Add-util-msys2.-c-h-and-rebase-db-msys2.-c.patch
  #git am "${srcdir}"/0003-use-busybox-for-msys2-post-installs.patch
  git am "${srcdir}"/0004-Link-pacman-with-static-libraries.patch
  
  # Workaround for symlinks
  rm src/util/util-common.{h,c}
  cp src/{common,pacman}/ini.h
  cp src/{common,pacman}/ini.c
  
  cp {src/common,lib/libalpm}/ini.h
  cp {src/common,lib/libalpm}/ini.c
  
  cp src/{common,util}/util-common.h
  cp src/{common,util}/util-common.c

  autoreconf -fi
}

build() {
  cd ${srcdir}/${pkgname}
  ./autogen.sh

  export PKG_CONFIG="/usr/bin/pkg-config --static"
  ./configure \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-static \
    --disable-shared \
    --enable-doc \
    --enable-doxygen \
    --enable-git-version \
    --with-scriptlet-shell=/usr/bin/bash \
    --with-pkg-ext=.pkg.tar.xz
  make -j1
  make -C contrib
}

check() {
  make -C "${pkgname}" check
}

package() {
  cd ${srcdir}/${pkgname}
  make -j1 DESTDIR=${pkgdir} install
  make -j1 DESTDIR=${pkgdir} -C contrib install

  # install Arch specific stuff
  install -dm755 ${pkgdir}/etc
  install -m644 ${srcdir}/pacman.conf ${pkgdir}/etc/pacman.conf

  case "$CARCH" in
  i686)
    mycarch="i686"
    mychost="i686-pc-msys"
    myflags="-march=i686"
  ;;
  x86_64)
    mycarch="x86_64"
    mychost="x86_64-pc-msys"
    myflags="-march=x86-64"
  ;;
  esac

  install -m644 ${srcdir}/makepkg.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw32.conf ${pkgdir}/etc/
  install -m644 ${srcdir}/makepkg_mingw64.conf ${pkgdir}/etc/
  install -m755 ${srcdir}/makepkg-mingw ${pkgdir}/usr/bin/

  # set things correctly in the default conf file
  sed -i ${pkgdir}/etc/makepkg.conf \
    -e "s|@CARCH[@]|$mycarch|g" \
    -e "s|@CHOST[@]|$mychost|g" \
    -e "s|@CARCHFLAGS[@]|$myflags|g"

  # put bash_completion in the right location
  install -dm755 ${pkgdir}/usr/share/bash-completion/completions
  mv ${pkgdir}/etc/bash_completion.d/pacman \
    ${pkgdir}/usr/share/bash-completion/completions
  rmdir ${pkgdir}/etc/bash_completion.d

  for f in makepkg pacman-key; do
    ln -s pacman "${pkgdir}/usr/share/bash-completion/completions/$f"
  done
}
